
const app = getApp();
const {
  getMiddleString
} = require('../../utils/jiequ');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    schoolexsls:"",
    EMPLOYEE_ID: '',
    openid: '',
    jiami: '',
    OPEN_ID: '',
    session_key: '',
    userinfo:{},
    sms:'',
    
    isSmsSend: false,     // 是否已经发送了验证码控制按钮禁用状态
    sendBtnText: '发送验证码', // 文本
    currentTime : 61, // 倒数60秒
  },
  submit(){
    if(this.data.sms != '' ){
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.interface+'/benJieMing/API_2303_SubmitStarCustomerRegister',
        method: 'POST',
        data: {
          mobile_num: this.data.userinfo.mobile_num,
          CODE: this.data.sms,
          EMPLOYEE_ID: this.data.EMPLOYEE_ID,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          wx.hideLoading()

       if(res.data.result == "success"){
        wx.showModal({
          title: '温馨提示',
         content: '登记成功',
         showCancel: false,
         confirmText: "我已知晓", 
              })
         }else{
          wx.showModal({
            title: '温馨提示',
           content: `${res.data.data}`,
           showCancel: false,
                })
         }

    
      
        },
        fail(err) { 
          console.error('请求失败', err)
          wx.hideLoading()

        }
      })
    }else{
      return;
    }

  },
  onSms(event){
    this.setData({
      sms:event.detail
    })
console.log('event.detail',event.detail);
  },
  sendSmsCode() {
    console.log('sendSmsCode');
    const that = this;
  
    //验证手机号和信息是否完整
    if (this.data.userinfo.mobile_num) {
   console.log('sthat.data.userinfo.mobile_num',that.data.userinfo.mobile_num);
  // 发起验证码请求
  wx.request({
    url: app.globalData.interface+'/app/API_1052_sendMassage',
    method: 'GET',
    data: {
      phone: this.data.userinfo.mobile_num,
      KEY: 'xianjgjKey9999',
      OPEN_ID: app.globalData.openid, //用户OPEN_ID
      session_key: this.data.session_key,
      OPEN_UUID: this.data.jiami
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: res => {

      if (res.result === 'error') {
        wx.showModal({
          title: '提示',
          content: `${res.code}`,
        });
        return;
      }

      // 成功后开始倒计时
      let currentTime = that.data.currentTime;

      // 禁用按钮
      that.setData({
        isSmsSend: true,
        sendBtnText: `${currentTime}秒后发送`
      });

      const interval = setInterval(() => {
        currentTime--;

        that.setData({
          sendBtnText: `${currentTime}秒后发送`
        });

        if (currentTime <= 0) {
          clearInterval(interval);
          that.setData({
            sendBtnText: '重新发送',
            currentTime: 61,
            isSmsSend: false // 恢复按钮可点击
          });
        }
      }, 1000);
    },
    fail(err) { 
      console.error('请求失败', err)
      wx.showToast({
        title: '发送失败',
        icon: 'none'
      });
    }
  })
    }else{
      wx.showToast({
        title: '请扫描客户二维码！',
        icon: 'error',
        duration: 2000
      });
    }
  
 
  },

  download(){
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.interface+'/benJieMing/API_2304_DownloadStarCustomer',
      method: 'get',
      data: {
        id: 61000026
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.hideLoading()
        console.log(res.data.data);
        if (res.data.require == "success") {
          wx.showModal({
            title: "导出失败",
          })
        } else {
          console.log('res', res.data.data);
          this.setData({
            schoolexsls: res.data.data,
          })



          this.exportdao()


        }
      },
      fail:res => {
        wx.hideLoading()
        console.log("超时")
    
      }
    })
  },
  // 导出
  exportdao() {
    console.log(11);
    let that = this
    const auth = wx.getStorageSync("auth");
    wx.showLoading({
      title: "加载中",
    });
    wx.downloadFile({
      //下载
      url: that.data.schoolexsls,
      header: {
        "content-type": "application/json",
      },
      success(res) {
        console.log(res);
        if (res.statusCode === 200) {
          let savePath =  wx.env.USER_DATA_PATH + "/TESUN_PLAN_" + Date.now() + '.xlsx';
          wx.getFileSystemManager().saveFile({
            //下载成功后保存到本地
            tempFilePath: res.tempFilePath,
            filePath: savePath,
            success(res) {
              wx.hideLoading();
              console.log(res);
              let savePath = res.savedFilePath;
              wx.showModal({
                title: "下载成功",
                content: "是否打开?",
                confirmColor: "#0bc183",
                confirmText: "打开",
                success(res) {
                  if (res.confirm) {
                    wx.openDocument({
                      //打开
                      filePath: savePath,
                      showMenu: true,
                      success(res) {
                        console.log(res);
                      },
                    });
                  } else if (res.cancel) {}
                },
              });
            },
            fail(err) {
              wx.hideLoading();
              wx.showModal({
                title: '温馨提示',
                content: that.data.schoolexsls,
                showCancel: false, //是否显示取消按钮
                success: function (result) {}
              })
            },
          });
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '温馨提示',
            content: that.data.schoolexsls,
            showCancel: false, //是否显示取消按钮
            success: function (result) {}
          })
        }
      },
      fail(err) {
        wx.hideLoading();
        wx.showModal({
          title: '温馨提示',
          content: that.data.schoolexsls,
          showCancel: false, //是否显示取消按钮
          success: function (result) {}
        })
      }
    });
  },
  gokehu(){

    // wx.navigateTo({
    //   url: "/pages/newyueyeuxiangkehuchauxun/newyueyeuxiangkehuchauxun",
    // })
  },

  saomiao() {

    this.setData({
      EMPLOYEE_ID: app.globalData.mealsList,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    });

    console.log(this.data.OPEN_ID);

    let that = this


    wx.scanCode({ //扫描API
      success(res) {
        console.log(res);
        console.log(res.result);

        const yonghuid = getMiddleString(res.result, '68581', 'MUQZ4GaM_')
        const qianop = getMiddleString(res.result, 'MUQZ4GaM_', 'Ns1S2b7UX4k')
        const houop = getMiddleString(res.result, 'Ns1S2b7UX4k', '4AN9Y')

        if (res.result != '') {
          wx.request({
            url: app.globalData.interface+'/benJieMing/API_2302_QueryStarCustomer',
            method: 'POST',
            data: {
              CODE: 'CUSTOMER_ID=' + yonghuid + '&' + 'openid=' + qianop + houop,
              EMPLOYEE_ID: that.data.EMPLOYEE_ID,
              OPEN_ID: that.data.OPEN_ID, //用户OPEN_ID
              session_key: that.data.session_key,
              OPEN_UUID: that.data.jiami,

            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
              console.log('输出回调信息',res.data.data )
          
              if(res.data.result ==  "success" ){
                that.setData({
                  userinfo:res.data.data
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: `${res.data.data}`,
                });
              }
          

        
            
            }
          })

        } else {

          wx.showToast({
            title: '扫描失败请重新扫码',
            duration: 1000
          })

        }

      }
    })

  },













  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(e.data);

 
    

    this.setData({
      EMPLOYEE_ID: e.data,
      EMPLOYEE_ID: app.globalData.mealsList,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,


    })




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})